package com.example.demon.utils;

import com.example.demon.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.stereotype.Component;

/**
 * 具体需求操作方法可百度
 */
@Component
public class RedisUtil {

    //只能操作字符串
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //能够操作对象，对象需要继承序列化接口
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 将健值存入redis缓存
     * @param key
     * @param value
     */
    public void setKeyValue(String key,String value){
        stringRedisTemplate.opsForValue().set(key, value);
    }

    /**
     * 取缓存中键为key的值
     * @param key
     * @return
     */
    public String getValueByKey(String key){
        String value = stringRedisTemplate.opsForValue().get(key);
        return value;
    }

    /**
     * 这只是个案例操作RedisTemplate，取值方法类似
     * @param key
     * @param admin
     */
    public void setKeyObject(String key, Admin admin){

        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.opsForValue().set("admin",admin);
    }

}
